object Demo16Match {
  def main(args: Array[String]): Unit = {
    /**
     * 模式匹配：
     *
     * Java中的模式匹配-->switch:
     * 1、基本数据类型
     * 2、字符串
     * 3、枚举
     *
     * Scala中的模式匹配-->match:
     * 可以有返回值
     * 1、基本数据类型
     * 2、字符串
     * 3、枚举
     * 4、类型
     * 5、样例类对象
     */

    // 1、匹配基本数据类型
    val i: Int = 4

    i match {
      case 1 =>
        println("i的值为1")
      case 2 =>
        println("i的值为2")
      case 3 =>
        println("i的值为3")
      case _ =>
        println("其他情况")
    }

    // 2、匹配字符串
    val str: String = "aaa"
    val matchRes: String = str match {
      case "abc" =>
        "abc".toUpperCase()
      case "efg" =>
        "efg".toUpperCase()
      case _ =>
        "other".toUpperCase()
    }
    println(matchRes)

    // 3、匹配枚举
    MyEnum.Wednesday match {
      case MyEnum.Monday =>
        println("今天是星期一")
        val monday: MyEnum.Value = MyEnum.Monday
        println(monday)
      case MyEnum.Tuesday =>
        println("今天是星期二")
        val tuesday: MyEnum.Value = MyEnum.Tuesday
        println(tuesday)
      case MyEnum.Wednesday =>
        println("今天是星期三")
        val wednesday: MyEnum.Value = MyEnum.Wednesday
        println(wednesday)
      case _ =>
        println("其他情况")
    }

    // 4、匹配类型
    //    val j: Any = "100"
    //    val j: Any = 100
    val j: Any = Stu("1", "zs", 20, "男", "一班")

    j match {
      case str: String =>
        println("j的类型为String")
      case int: Int =>
        println("j的类型为Int")
      case stu: Stu =>
        println("j的类型为Stu")
    }

    // 5、匹配样例类对象
    val stu01: Stu = Stu("3", "zs3", 22, "男", "一班")

    stu01 match {
      case Stu("2", "zs1", 22, "男", "一班") =>
        println(stu01.name)
      case Stu("3", "zs3", 22, "男", "一班") =>
        println(stu01.name)
      case Stu("4", "zs4", 22, "男", "一班") =>
        println(stu01.name)
    }

    // 模式匹配的应用

    // 1、在Map中通过Key获取Value

    // 2、通过模式匹配简化map中的取值逻辑
    val stuList: List[(Int, (String, Int, (String, String)))] = List[(Int, (String, Int, (String, String)))](
      (1, ("张三1", 21, ("男", "文科一班"))),
      (2, ("张三2", 22, ("女", "文科二班"))),
      (3, ("张三3", 23, ("男", "理科三班"))),
      (4, ("张三4", 24, ("女", "理科四班")))
    )

    // 将stuList展开（将List中的每个元素转成字符串并输出）
    // 例如：(1, ("张三1", 21, ("男", "文科一班"))) --> 1,张三1,21,男,文科一班
    stuList.map(t2 => {
      val id: Int = t2._1
      val tt3: (String, Int, (String, String)) = t2._2
      val name: String = tt3._1
      val age: Int = tt3._2
      val tt3_t2: (String, String) = tt3._3
      val gender: String = tt3_t2._1
      val clazz: String = tt3_t2._2
      s"$id,$name,$age,$gender,$clazz"
    }).foreach(println)

    stuList.map {
      case (id:Int, (name:String, age:Int, (gender:String, clazz:String)))=>
        s"$id,$name,$age,$gender,$clazz"
    }.foreach(println)


  }

}

// 定义一个枚举 保存一个星期的七天
object MyEnum extends Enumeration {
  val Monday = Value("Mo.")
  val Tuesday = Value("Tu.")
  val Wednesday = Value("We.")
  val Thursday = Value("Th.")
  val Friday = Value("Fr.")
  val Saturday = Value("Sa.")
  val Sunday = Value("Su.")
}
